package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.LoanApiOrderRepaymentRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.LoanApiOrderRepaymentSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanApiOrderRepaymentSearchResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.Select;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanWholeRepayDetail.LOAN_WHOLE_REPAY_DETAIL;

@Repository
public class LoanApiOrderRepaymentRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanApiOrderRepaymentRepository {

    @Override
    protected TableImpl getTable() {
        return LOAN_WHOLE_REPAY_DETAIL;
    }

    @Override
    public Page<LoanApiOrderRepaymentSearchResponse> list(LoanApiOrderRepaymentSearchRequest request, Pageable pageable) {
        Select sql1=
                dslContext.select(LOAN_WHOLE_REPAY_DETAIL.CURRENT_PERIOD,
                LOAN_WHOLE_REPAY_DETAIL.ORDER_ID,
                LOAN_WHOLE_REPAY_DETAIL.CURRENT_FLAG,
                LOAN_WHOLE_REPAY_DETAIL.REPAY_DATE,
                LOAN_WHOLE_REPAY_DETAIL.PLAN_REPAY_DATE,
                LOAN_WHOLE_REPAY_DETAIL.REAL_REPAY_DATE,
                LOAN_WHOLE_REPAY_DETAIL.REPAY_STATUS,
                LOAN_WHOLE_REPAY_DETAIL.EXCEED_STATUS,
                LOAN_WHOLE_REPAY_DETAIL.REAL_PAY_AMOUNT,
                LOAN_WHOLE_REPAY_DETAIL.PLAN_REPAY_AMT,
                LOAN_WHOLE_REPAY_DETAIL.INTEREST_AMT,
                LOAN_WHOLE_REPAY_DETAIL.SERVICE_FEE,
                LOAN_WHOLE_REPAY_DETAIL.OVER_DUE_FEE)
                .from(LOAN_WHOLE_REPAY_DETAIL)
                .where(LOAN_WHOLE_REPAY_DETAIL.ORDER_ID.eq(request.getOrderId()))
                .orderBy(LOAN_WHOLE_REPAY_DETAIL.PLAN_REPAY_DATE.desc());
        Select sql2 = dslContext.select(
                DSL.field(DSL.ifnull(null,"总计")).as("总计"),
                DSL.field(DSL.ifnull(null,"")).as("1"),
                DSL.field(DSL.ifnull(null,"")).as("2"),
                DSL.field(DSL.ifnull(null,"")).as("3"),
                DSL.field(DSL.ifnull(null,"")).as("4"),
                DSL.field(DSL.ifnull(null,"")).as("5"),
                DSL.field(DSL.ifnull(null,"")).as("6"),
                DSL.field(DSL.ifnull(null,"")).as("7"),
                DSL.sum(DSL.cast(DSL.field(LOAN_WHOLE_REPAY_DETAIL.REAL_PAY_AMOUNT),SQLDataType.DECIMAL(7,2))),
                DSL.sum(DSL.cast(DSL.field(LOAN_WHOLE_REPAY_DETAIL.PLAN_REPAY_AMT),SQLDataType.DECIMAL(7,2))),
                DSL.sum(DSL.cast(DSL.field(LOAN_WHOLE_REPAY_DETAIL.INTEREST_AMT),SQLDataType.DECIMAL(7,2))),
                DSL.sum(DSL.cast(DSL.field(LOAN_WHOLE_REPAY_DETAIL.SERVICE_FEE),SQLDataType.DECIMAL(7,2))),
                DSL.sum(DSL.cast(DSL.field(LOAN_WHOLE_REPAY_DETAIL.OVER_DUE_FEE),SQLDataType.DECIMAL(7,2))))
                .from(LOAN_WHOLE_REPAY_DETAIL)
                .where(LOAN_WHOLE_REPAY_DETAIL.ORDER_ID.eq(request.getOrderId()));
         SelectQuery sql = dslContext.select().from(sql1.unionAll(sql2)).getQuery();

        return search(sql,pageable,record -> {
            return getResponse(record);
        });
    }

    private LoanApiOrderRepaymentSearchResponse getResponse(Record record){
        LoanApiOrderRepaymentSearchResponse response = new LoanApiOrderRepaymentSearchResponse();
        if ("总计".equals(record.get(LOAN_WHOLE_REPAY_DETAIL.CURRENT_PERIOD))){
            response.setCurrentPeriod(record.get(LOAN_WHOLE_REPAY_DETAIL.CURRENT_PERIOD));
        }else{
            response.setCurrentPeriod(record.get(LOAN_WHOLE_REPAY_DETAIL.CURRENT_PERIOD)+"期");
        }
        response.setRepayDate(record.get(LOAN_WHOLE_REPAY_DETAIL.REPAY_DATE));
        response.setPlanRepayDate(record.get(LOAN_WHOLE_REPAY_DETAIL.PLAN_REPAY_DATE));
        response.setRealRepayDate(record.get(LOAN_WHOLE_REPAY_DETAIL.REAL_REPAY_DATE));
        response.setRepayStatus(record.get(LOAN_WHOLE_REPAY_DETAIL.REPAY_STATUS));
        response.setRealPayAmount(record.get(LOAN_WHOLE_REPAY_DETAIL.REAL_PAY_AMOUNT));
        response.setPlanRepayAmt(record.get(LOAN_WHOLE_REPAY_DETAIL.PLAN_REPAY_AMT));
        response.setInterestAmt(record.get(LOAN_WHOLE_REPAY_DETAIL.INTEREST_AMT));
        response.setServiceFee(record.get(LOAN_WHOLE_REPAY_DETAIL.SERVICE_FEE));
        response.setOverDueFee(record.get(LOAN_WHOLE_REPAY_DETAIL.OVER_DUE_FEE));
        response.setOrder_id(record.get(LOAN_WHOLE_REPAY_DETAIL.ORDER_ID));
        response.setCurrent_flag(record.get(LOAN_WHOLE_REPAY_DETAIL.CURRENT_FLAG));
        response.setExceedStatus(StringUtils.isNotBlank(record.get(LOAN_WHOLE_REPAY_DETAIL.EXCEED_STATUS))?Integer.parseInt(record.get(LOAN_WHOLE_REPAY_DETAIL.EXCEED_STATUS)):3);
        return response;
    }
}
